package com.xm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xm.pojo.Orders;
import com.xm.pojo.User;
import com.xm.service.OrdersService;
import com.xm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表现层（web层）：接收前端传递的数据，返回数据给前端
 *
 * @Controller，@RestController，@Service，@Repository创建对象所需的注解
 */
@RestController//返回json类型的数据
@RequestMapping("orders")//一级访问路径
public class OrdersController {
    @Autowired //自动装配，将spring容器中的PatientService类型对象，赋值给patientService
    private OrdersService ordersService;

    @RequestMapping("findOrders")//查找所有用户
    public Map findOrders() {
        List<Orders> orders = ordersService.findOrders();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", 1000);
        map.put("data", orders);
        return map;
    }

    @RequestMapping("findByPage")//分页显示接口
    public Map findByPage(@RequestParam(defaultValue = "") String p,
                          @RequestParam(defaultValue = "") String l,
                          @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "limit", required = false) Integer pageSize) {

        List<Orders> list = null;
        if (p.equals("")) {
            PageHelper.startPage(pageNum, pageSize);
            list = ordersService.findOrders();
            PageInfo<Object> pageInfo = new PageInfo<Object>(list);
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", 0);
            map.put("msg", "success");
            map.put("count", pageInfo.getTotal());//总记录数
            map.put("data", pageInfo.getList());//分页数据
            return map;
        }
        if (!p.equals("")) {
            if (l.equals("1")) {
                PageHelper.startPage(pageNum, pageSize);
                list = ordersService.findGid(p);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (l.equals("2")) {
                PageHelper.startPage(pageNum, pageSize);
                list = ordersService.findUid(p);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (l.equals("3")) {
                PageHelper.startPage(pageNum, pageSize);
                list = ordersService.findAmount(p);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (l.equals("4")) {
                PageHelper.startPage(pageNum, pageSize);
                list = ordersService.findOtype(p);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            } else {
                PageHelper.startPage(pageNum, pageSize);
                list = ordersService.findOrders();
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
        } else {
            PageHelper.startPage(pageNum, pageSize);
            list = ordersService.findOrders();
            PageInfo<Object> pageInfo = new PageInfo<Object>(list);
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", 0);
            map.put("msg", "success");
            map.put("count", pageInfo.getTotal());//总记录数
            map.put("data", pageInfo.getList());//分页数据
            return map;
        }
    }

    @RequestMapping("add")//添加接口
    public void add(Orders orders, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //System.out.println(orders);

        ordersService.add(orders);

        //添加完之后，跳转到order页面
        response.sendRedirect(request.getContextPath() + "/order.html");
    }

    @RequestMapping("updateOrder")//编辑接口
    public String updateOrder(Orders orders) {
        System.out.println(orders);
        ordersService.updateOrder(orders);
        return "success";

    }

    @RequestMapping("logicDelete")//逻辑删除接口
    public String logicDelete(Orders orders) {
        System.out.println(orders.getOid());
        ordersService.logicDelete(orders);
        return "success";
    }

    @RequestMapping("batchDel")//批量物理删除接口
    public String batchDel(Integer[] ids) {
        ordersService.batchDel(ids);
        return "success";
    }


    @RequestMapping("getordersNum")//二级访问路径，获取确诊人数
    public Integer getordersNum() {
        Integer ordersNum = ordersService.getordersNum();
        return ordersNum;
    }
}
